package com.qtummatrix.dao.impl;

import com.qtummatrix.dao.ILoansDao;
import com.qtummatrix.entity.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * create by Gongshiyong  2019-11-21 19:39
 */

@Repository("loansDao")

public class LoansDaoImpl implements ILoansDao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * 查询产品详情
     *
     * @return
     */
    @Override
    public LoanproductsEntity particular(int id) {
        String sql = "select * from LoanproductsEntity where id = :id";
        Query query = getSession().createQuery(sql).setParameter("id", id);
        LoanproductsEntity loanproductsEntity = (LoanproductsEntity) query.uniqueResult();
        return loanproductsEntity;
    }

    /**
     * 我的贷款 登录
     *
     * @param name
     * @param UID
     * @return
     */
    @Override
    public String login(String name, String UID) {
        String sql = "from CustomerinfoEntity where customername = ? and uid = ?";
        Session session = getSession();
        String str = "";
        Query query = session.createQuery(sql);
        query.setParameter(0, name);
        query.setParameter(1, UID);
        if (query.uniqueResult() != null) {
            str = "登录成功！";

        } else {
            str = "登录失败！";
        }
        return str;
    }

    /**
     * 客户信息
     *
     * @param
     * @return
     */
    @Override
    public CustomerinfoEntity getMessage(String name, String UID) {
        String sql = "from CustomerinfoEntity where customername = ? and uid = ?";
        Query query = getSession().createQuery(sql);
        query.setParameter(0, name);
        query.setParameter(1, UID);
        CustomerinfoEntity customerinfoEntity = (CustomerinfoEntity) query.uniqueResult();
        return customerinfoEntity;
    }

    /**
     * 贷款信息表
     *
     * @param username
     * @return
     */
    @Override
    public ContractEntity showMessage(String username) {
        String sql1 = "from ContractEntity where openbankid = ?";
        Session session = getSession();
        Query query = session.createQuery(sql1);
        query.setParameter(0, username);
        ContractEntity contractEntity = (ContractEntity) query.uniqueResult();
        return contractEntity;
    }

    /**
     * 查询调查问卷
     *
     * @return
     */
    @Override
    public List<CustomeranswerEntity> questionnaire() {
        List<CustomeranswerEntity> list = new ArrayList<CustomeranswerEntity>();
        return null;
    }

    /**
     * 查询模组
     *
     * @return
     */
    @Override
    public List<Integer> question() {
        List<Integer> list = new ArrayList<Integer>();
        String sql = "select issueId from MoudleQuestionEntity where moudleId = ?";
        Session session = getSession();
        Query query = session.createQuery(sql);
        query.setParameter(0, 1);
        list = query.list();
        return list;
    }

    @Override
    public List<QuestionsEntity> getQuestr(List<Integer> list) {
        List<QuestionsEntity> list1 = new ArrayList<QuestionsEntity>();
        String sql = "from QuestionsEntity where id in(:list)";
        Session session = getSession();
        Query query = session.createQuery(sql);
        query.setParameterList("list", list);
        list1 = query.list();
        return list1;
    }

    /**
     * 查询分数
     *
     * @param arr
     * @return
     */
    @Override
    public int getGrode(String[] arr, List<Integer> list) {
        String sql = "from QuestionsEntity where id = ?";
        Session session = getSession();
        Query query = session.createQuery(sql);
        List<Integer> list1 = new ArrayList<Integer>();
        List<QuestionsEntity> questionsEntityList=new ArrayList<QuestionsEntity>();
        for (int i = 0; i < 10; i++) {
            query.setParameter(0, list.get(i));
            questionsEntityList.add((QuestionsEntity) query.uniqueResult());
        }
        for (int i=0;i<10;i++){
            if (arr[i].equals("A")){
                list1.add(questionsEntityList.get(i).getA());
            }
           else if (arr[i].equals("B")){
                list1.add(questionsEntityList.get(i).getB());
            }
           else if (arr[i].equals("C")){
                list1.add(questionsEntityList.get(i).getC());
            }
            else {
                list1.add(questionsEntityList.get(i).getD());
            }
        }

        int sum = 0;
        for (Integer list2 : list1) {
            sum += list2;
        }
        return sum;
    }

    /**
     * 信用评级
     *
     * @param sum
     * @return
     */
    @Override
    public String getRate(int sum) {
        String sql = "select creditgrade from CreditgradeEntity where minGrade <= ? and maxGrade >= ?";
        Session session = getSession();
        Query query = session.createQuery(sql);
        query.setParameter(0, sum);
        query.setParameter(1, sum);
        String count = (String) query.uniqueResult();
        return count;
    }

    /**
     * 信用评级加入到客户表
     *
     * @param role
     * @return
     */
    @Override
    public void addInfo(String role, String username) {
        Session session = getSession();
        String sql = "from CustomerinfoEntity where customername = ?";
        Query query = session.createQuery(sql);
        query.setParameter(0, username);
        CustomerinfoEntity customerinfoEntity = new CustomerinfoEntity();
        customerinfoEntity = (CustomerinfoEntity) query.uniqueResult();
        customerinfoEntity.setCreditgrade(role);
        session.saveOrUpdate(customerinfoEntity);
    }

    /**
     * 查询客户申请表
     *
     * @param applicationEntity
     * @return
     */
    @Override
    public String queryUser(ApplicationEntity applicationEntity) {
        Session session = getSession();
        String str = "";
        String hql = "from ApplicationEntity where customename = ? and customeuid = ? and customephone = ?";
        Query query = session.createQuery(hql);
        query.setParameter(0, applicationEntity.getCustomename());
        query.setParameter(1, applicationEntity.getCustomeuid());
        query.setParameter(2, applicationEntity.getCustomephone());
        if (query.uniqueResult() == null) {
            str = "用户不存在！";
        } else {
            str = "用户存在！";
        }
        return str;
    }

    /**
     * 添加客户申请
     *
     * @return
     */
    @Override
    public String addUser(ApplicationEntity applicationEntity) {
        Session session = getSession();
        String str = "";
        session.save(applicationEntity);
        str = "添加成功！";
        return str;
    }

    @Override
    public void addInfos(CustomerinfoEntity customerinfoEntity) {
        Session session = getSession();
        session.save(customerinfoEntity);
    }

    @Override
    public String getInfos(CustomerinfoEntity customerinfoEntity) {
        Session session = getSession();
        String sql = "from CustomerinfoEntity where customername = ? and uid = ?";
        Query query = session.createQuery(sql);
        query.setParameter(0, customerinfoEntity.getCustomername());
        query.setParameter(1, customerinfoEntity.getUid());
        String str = "";
        if (query.uniqueResult() == null) {
            str = "客户表中，用户不存在！";
        } else {
            str = "客户表中，用户已存在！";
        }
        return str;
    }

    @Override
    public String getBlack(CustomerinfoEntity customerinfoEntity) {
        Session session = getSession();
        String str = "";
        String sql = "from BlackList where name = ? and UID = ?";
        Query query = session.createQuery(sql);
        query.setParameter(0, customerinfoEntity.getCustomername());
        query.setParameter(1, customerinfoEntity.getUid());
        if (query.uniqueResult() != null) {
            str = "用户存在！";
        } else {
            str = "用户不存在！";
        }
        return str;
    }

    /**
     * 查询黑名单
     *
     * @return
     */
    @Override
    public String showUser(ApplicationEntity applicationEntity) {
        Session session = getSession();
        String str = "";
        String sql = "from BlackList where name = ? and UID = ?";
        Query query = session.createQuery(sql);
        query.setParameter(0, applicationEntity.getCustomename());
        query.setParameter(1, applicationEntity.getCustomeuid());
        if (query.uniqueResult() != null) {
            str = "用户存在！";
        } else {
            str = "用户不存在！";
        }
        return str;
    }
}
